package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_029A;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_029B;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *@Description: 当在“总部审批”节点，批准通过时，根据主表中的“人事范围编码”、“年份”、“节日类型”或“人事范围编码”、“年份”、“节日类型”、“部门编码”
 * （看人事范围的值是否等于“8004” 或“8007” 或“ZB01”）；将uf_jbfxfh中的“zt”字段改为2；并将流程主表中的BMSBJRJBF更新到uf_jbfxfhz中的BMSBJRJBF；
 * 将流程主表中的BMSBXJJBF更新到uf_jbfxfhz中的BMSBXJJBF；
 * 将流程明细表中的GRSBJRJBF、GRSBXJJBF更新到uf_jbfxfmx中的GRSBJRJBF、GRSBXJJBF中
 *@Author: Dusongsong
 *@Date: 2019/12/24
 */
public class WeaHRjbftsAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        RecordSet rs4 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用: WeaHRjbftsAction");
       // String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String workflowid = requestInfo.getWorkflowid();
        rs.executeQuery("select b.tablename from workflow_base a,workflow_bill b where  b.id=a.formid  and a.id =?", workflowid);
        rs.next();
        String tablename = Util.null2String(rs.getString("tablename"));//表名
        //查询主表
        String STATE = "2";
        List<Map<String, String>> list = new ArrayList<>();
        ZFM_HR_029B hr_029B = new ZFM_HR_029B();
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("select * from " + tablename + " where requestid=" + requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));//外键
        String sqr = Util.null2String(rs.getString("sqr"));//申请人
        String GJAHR = Util.null2String(rs.getString("GJAHR"));       //年度
        String JRLX = Util.null2String(rs.getString("JRLX"));         //节日类型
        String WERKS = Util.null2String(rs.getString("WERKS"));       //人事范围
        String BMBM = Util.null2String(rs.getString("BMBM"));         //部门编码
        if ("8004".equals(WERKS) || "8007".equals(WERKS) || "ZB01".equals(WERKS)) {
            rs4.executeQuery(" select distinct case when b.field7='ZB01' OR b.field7='8004' or b.field7='8007' then b.field2 when b.field7 not in('ZB01','8004','8007') then c.departmentcode end departmentcode from hrmresource a left join cus_fielddata b on a.id=b.id left join hrmdepartment c on a.departmentid=c.id where a.id='"+sqr+"'");
            rs4.next();
            String sqrid = Util.null2String(rs4.getString("departmentcode"));//部门编码
            baseBean.writeLog("部门编码"+sqrid);
            String BMGDJRJBF = Util.null2String(rs.getString("BMGDJRJBF"));         //部门规定节日加班费
            String JRZBBL = Util.null2String(rs.getString("JRZBBL"));         //节日值班比例
            String BMGDXJJBF = Util.null2String(rs.getString("BMGDXJJBF"));         //部门规定现金加班费
            String BMSBJRJBF = Util.null2String(rs.getString("BMSBJRJBF"));         //部门上报节日加班费
            String BMSBXJJBF = Util.null2String(rs.getString("BMSBXJJBF"));         //部门上报现金加班费
            Map<String, String> map1 = new HashMap();
            map1.put("GJAHR", GJAHR);
            map1.put("JRLX", JRLX);
            map1.put("WERKS", WERKS);
            map1.put("BMBM", sqrid);
            map1.put("BMGDJRJBF", BMGDJRJBF);
            map1.put("JRZBBL", JRZBBL);
            map1.put("BMGDXJJBF", BMGDXJJBF);
            map1.put("BMSBJRJBF", BMSBJRJBF);
            map1.put("BMSBXJJBF", BMSBXJJBF);
            baseBean.writeLog("相关参数:" + map1.toString());
            //明细表
            String sqlmx = "select numid,pernr,grsbjrjbf,grsbxjjbf  from " + tablename + "_dt1 where mainid='" + mainid + "' and werks='" + WERKS + "' and gjahr='" + GJAHR + "' and jrlx='" + JRLX + "' and bmbm='" + BMBM + "'";
            baseBean.writeLog(sqlmx);
            rs2.execute(sqlmx);
            try {
                while (rs2.next()) {
                    String NUMID = Util.null2String(rs2.getString("NUMID"));//唯一标识
                    String PERNR = Util.null2String(rs2.getString("PERNR"));//人员编码
                    String GRSBJRJBF = Util.null2String(rs2.getString("GRSBJRJBF"));//个人上报节日加班费
                    String GRSBXJJBF = Util.null2String(rs2.getString("GRSBXJJBF"));//个人上报现金加班费
                    Map<String, String> map2 = new HashMap();
                    map2.put("NUMID", NUMID);
                    map2.put("GJAHR", GJAHR);
                    map2.put("JRLX", JRLX);
                    map2.put("PERNR", PERNR);
                    map2.put("GRSBJRJBF", GRSBJRJBF);
                    map2.put("GRSBXJJBF", GRSBXJJBF);
                    list.add(map2);
                    baseBean.writeLog("相关参数:" + map2.toString());
                }
                    Map resultMap = hr_029B.execute(list);
                    String flag = (String) resultMap.get("flag");
                    String message = (String) resultMap.get("message");
                    baseBean.writeLog("SAP返回结果——flag:" + flag + ";message:" + message);
                    if ("s".equalsIgnoreCase(flag)) {
                        rs2.execute(sqlmx);
                        while (rs2.next()) {
                            String NUMID = Util.null2String(rs2.getString("NUMID"));//唯一标识
                            String GRSBJRJBF = Util.null2String(rs2.getString("GRSBJRJBF"));//个人上报节日加班费
                            String GRSBXJJBF = Util.null2String(rs2.getString("GRSBXJJBF"));//个人上报现金加班费
                            String updateSQL = "update uf_jbfmxb set grsbjrjbf='" + GRSBJRJBF + "' ,grsbxjjbf= '" + GRSBXJJBF + "' where  werks='" + WERKS + "' and gjahr='" + GJAHR + "' and jrlx='" + JRLX + "' and numid='"+NUMID+"'";
                            rs3.executeUpdate(updateSQL);
                            baseBean.writeLog("调用SQL" + updateSQL);
                        }
                    } else {
                        requestInfo.getRequestManager().setMessageid("111100");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                ZFM_HR_029A hr_029A = new ZFM_HR_029A();
                Map resultMap1 = hr_029A.execute(map1);
                String flag1 = (String) resultMap1.get("flag");
                String message1 = (String) resultMap1.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag1 + ";message:" + message1);
                if ("s".equalsIgnoreCase(flag1)) {
                    String updateSQL1 = "update uf_jbfhzb set bmsbjrjbf='" + BMSBJRJBF + "',bmsbxjjbf='" + BMSBXJJBF + "',zt='" + STATE + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and jrlx='" + JRLX + "' and bmbm='" + BMBM + "'";
                    rs.executeUpdate(updateSQL1);
                    baseBean.writeLog("执行语句：" + updateSQL1);
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message1);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } catch (Exception e) {
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } else {
            String BMGDJRJBF = Util.null2String(rs.getString("BMGDJRJBF"));         //部门规定节日加班费
            String JRZBBL = Util.null2String(rs.getString("JRZBBL"));         //节日值班比例
            String BMGDXJJBF = Util.null2String(rs.getString("BMGDXJJBF"));         //部门规定现金加班费
            String BMSBJRJBF = Util.null2String(rs.getString("BMSBJRJBF"));         //部门上报节日加班费
            String BMSBXJJBF = Util.null2String(rs.getString("BMSBXJJBF"));         //部门上报现金加班费
            Map<String, String> map1 = new HashMap();
            map1.put("GJAHR", GJAHR);
            map1.put("JRLX", JRLX);
            map1.put("WERKS", WERKS);
            map1.put("BMBM", "00000000");
            map1.put("BMGDJRJBF", BMGDJRJBF);
            map1.put("JRZBBL", JRZBBL);
            map1.put("BMGDXJJBF", BMGDXJJBF);
            map1.put("BMSBJRJBF", BMSBJRJBF);
            map1.put("BMSBXJJBF", BMSBXJJBF);
            baseBean.writeLog("相关参数:" + map1.toString());
            //明细表
            String sqlmx = "select numid,pernr,grsbjrjbf,grsbxjjbf  from " + tablename + "_dt1 where mainid='" + mainid + "' and werks='" + WERKS + "' and gjahr='" + GJAHR + "' and jrlx='" + JRLX + "'";
            baseBean.writeLog(sqlmx);
            rs2.execute(sqlmx);
            try {
                while (rs2.next()) {
                    String NUMID = Util.null2String(rs2.getString("NUMID"));//唯一标识
                    String PERNR = Util.null2String(rs2.getString("PERNR"));//人员编码
                    String GRSBJRJBF = Util.null2String(rs2.getString("GRSBJRJBF"));//个人上报节日加班费
                    String GRSBXJJBF = Util.null2String(rs2.getString("GRSBXJJBF"));//个人上报现金加班费
                    Map<String, String> map2 = new HashMap();
                    map2.put("NUMID", NUMID);
                    map2.put("GJAHR", GJAHR);
                    map2.put("JRLX", JRLX);
                    map2.put("PERNR", PERNR);
                    map2.put("GRSBJRJBF", GRSBJRJBF);
                    map2.put("GRSBXJJBF", GRSBXJJBF);
                    list.add(map2);
                    baseBean.writeLog("相关参数:" + map2.toString());
                }
                Map resultMap = hr_029B.execute(list);
                String flag = (String) resultMap.get("flag");
                String message = (String) resultMap.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag + ";message:" + message);
                if ("s".equalsIgnoreCase(flag)) {
                    rs2.execute(sqlmx);
                    while (rs2.next()) {
                        String NUMID = Util.null2String(rs2.getString("NUMID"));//唯一标识
                        String GRSBJRJBF = Util.null2String(rs2.getString("GRSBJRJBF"));//个人上报节日加班费
                        String GRSBXJJBF = Util.null2String(rs2.getString("GRSBXJJBF"));//个人上报现金加班费
                        String updateSQL = "update uf_jbfmxb set grsbjrjbf='" + GRSBJRJBF + "' ,grsbxjjbf= '" + GRSBXJJBF + "' where  werks='" + WERKS + "' and gjahr='" + GJAHR + "' and jrlx='" + JRLX + "' and numid='"+NUMID+"'";
                        rs3.executeUpdate(updateSQL);
                        baseBean.writeLog("调用SQL" + updateSQL);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
                ZFM_HR_029A hr_029A = new ZFM_HR_029A();
                Map resultMap1 = hr_029A.execute(map1);
                String flag1 = (String) resultMap1.get("flag");
                String message1 = (String) resultMap1.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag1 + ";message:" + message1);
                if ("s".equalsIgnoreCase(flag1)) {
                    String updateSQL1 = "update uf_jbfhzb set bmsbjrjbf='" + BMSBJRJBF + "',bmsbxjjbf='" + BMSBXJJBF + "',zt='" + STATE + "' where werks='" + WERKS + "' and gjahr='" + GJAHR + "' and jrlx='" + JRLX + "'";
                    rs.executeUpdate(updateSQL1);
                    baseBean.writeLog("执行语句：" + updateSQL1);
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message1);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } catch (Exception e) {
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        }
    }
}
